package com.ht.api.call.impl;

import com.ht.api.db.DbConfig;
import org.springframework.stereotype.Component;

import com.ht.api.bean.AjaxResult;
import com.ht.api.bean.LoginBean;
import com.ht.api.call.AbstractCall;
import com.ht.api.call.ICall;
import com.ht.api.db.Db;
import com.ht.api.db.Entity;

import cn.hutool.json.JSONObject;

import javax.sql.DataSource;

/**
 * 业务执行器:add
 * @author asq
 * @createTime 2024年12月23日 21:02:18
 */
@Component
public class AddCall extends AbstractCall {
	
	@Override
	public boolean isOpen() {
		return false;
	}

	@Override
	public String getName() {
		return "通用新增";
	}
	
	@Override
	public AjaxResult doExecute(LoginBean me, JSONObject param, JSONObject config) {
		String table = config.getStr("table");
		Entity entity = param.getBean(PARAM, Entity.class).setTable(table).set("sys_creator", me.getLoginId());

		DataSource ds = null;
		if (me.isSysCommonCall()) {
			ds = DbConfig.useSysDb();
		} else {
			ds = me.getDataSource();
		}

		Db.insert(ds, entity);
		return AjaxResult.success().put(ICall.MAIN_ID, entity.getStr(ICall.MAIN_ID));
	}

}
